//
//  LXJToolBarView.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/31.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit

class LXJToolBarView: UIView {
    // MARK: 微博的模型属性,并在控件上显示内容
    var weiboCount: LXJWeiboViewModel? {
        didSet{
            /// 控件赋值
            /// 转发数量
            reposts.setTitle(weiboCount?.repostsCountStr, for: .normal)
            /// 评论数量
            comments.setTitle(weiboCount?.commentsCountStr, for: .normal)
            /// 点在数量
            attitudes.setTitle(weiboCount?.attitudesCountStr, for: .normal)
            
        }
    }
    
    // MARK: 控件的懒加载
    /// 工具条
    fileprivate lazy var reposts: UIButton = {
        let but = self.creatToolButton(titile: "转发", imageName: "timeline_icon_retweet")
        return but
    }()
    fileprivate lazy var comments: UIButton = {
        let but = self.creatToolButton(titile: "评论", imageName: "timeline_icon_comment")
        return but
    }()
    fileprivate lazy var attitudes: UIButton = {
        let but = self.creatToolButton(titile: "点赞", imageName: "timeline_icon_unlike")
        return but
    }()
    /// 分割线
    fileprivate lazy var firstLine: UIImageView = {
        let line = self.creatSeperateLine()
        return line
    }()
    fileprivate lazy var secondLine: UIImageView = {
        let line = self.creatSeperateLine()
        return line
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 背景颜色
        backgroundColor = UIColor.clear
        
        /// 自动布局
        reposts.snp.makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(comments)
        }
        
        comments.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(reposts.snp.trailing)
            make.width.equalTo(attitudes)
        }
        
        attitudes.snp.makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(comments.snp.trailing)
            make.width.equalTo(reposts)
        }
        
        firstLine.snp.makeConstraints { (make) in
            make.centerX.equalTo(reposts.snp.trailing)
            make.centerY.equalTo(reposts)
        }
        
        secondLine.snp.makeConstraints { (make) in
            make.centerX.equalTo(comments.snp.trailing)
            make.centerY.equalTo(comments)
        }
    }
    
    /// 摁扭的创建和添加
    fileprivate func creatToolButton(titile: String, imageName: String) -> UIButton {
        let but = UIButton()
        /// 设置文字
        but.setTitle(titile, for: .normal)
        /// 字体颜色
        but.setTitleColor(UIColor.darkGray, for: .normal)
        /// 字体字号
        but.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        /// 设置图片
        but.setImage(UIImage(named: imageName), for: .normal)
        /// 设置背景图片
        but.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        but.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), for: .highlighted)
        /// 添加到父控件
        addSubview(but)
        return but
    }
    
    /// 摁扭之间的分割线
    fileprivate func creatSeperateLine() -> UIImageView {
        let line = UIImageView()
        /// 设置图片
        line.image = UIImage(named: "timeline_card_bottom_line")
        /// 添加到父控件
        addSubview(line)
        return line
    }
}
